package org.czl.leetCode1;

public class L979 {
    public int distributeCoins(TreeNode root) {
        return dfs(root)[2]; //下标 0 节点总数 1 硬币总数 2移动多少步平
    }
    int[] dfs(TreeNode root){
        if(root == null)
            return new int[]{0,0,0};

        int[] left = dfs(root.left);
        int[] right = dfs(root.right);
        int sum = left[0] + right[0] + 1;
        int coin = left[1] + right[1] + root.val;
        int move = left[2] + right[2] + Math.abs(left[0] - left[1]) + Math.abs(right[0] - right[1]);
        return new int[]{sum, coin, move};
    }
}
